﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using ExpreRec.Logic.Preprocessing;
using ExpreRec.Logic.ImageAcquisition;
using System.IO;

namespace ExpreRec.UI
{
    public partial class MainForm : Form
    {
        private IList<Form> forms = new List<Form>();

        private FileImageReader fileImageReader = new FileImageReader();
        private CameraImageReader cameraImageReader = new CameraImageReader();

        public MainForm()
        {
            InitializeComponent();
        }

        public void ShowForm(Form form)
        {
            foreach (Form f in forms)
            {
                f.Close();
                f.Dispose();
            }

            forms.Clear();

            form.MdiParent = this;
            form.Visible = false;
            forms.Add(form);

            form.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void recognizeLocalPicture_Click(object sender, EventArgs e)
        {
            if (openFileDialogImage.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialogImage.CheckFileExists)
                {
                    IplImage image = null;
                    try
                    {
                        image = fileImageReader.GetImage(openFileDialogImage.FileName);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("图片格式错误！");
                    }

                    if (image != null)
                    {
                        LocalPictureRecognitionResultForm localPictureRecognitionResultForm = new LocalPictureRecognitionResultForm();


                        localPictureRecognitionResultForm.SetImage(image);
                        ShowForm(localPictureRecognitionResultForm);
                    }
                }
            }
        }

        private void recognizeCameraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CameraPictureRecognitionResultForm cameraPictureRecognitionResultForm = new CameraPictureRecognitionResultForm();

            ShowForm(cameraPictureRecognitionResultForm);
        }

        private void trainingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialogImages.ShowDialog() == DialogResult.OK)
            {
                if (Directory.Exists(folderBrowserDialogImages.SelectedPath))
                {
                    TrainingPreprocessForm form = new TrainingPreprocessForm();
                    form.FolderName = folderBrowserDialogImages.SelectedPath;
                    ShowForm(form);
                }
            }
        }

        private void paramsListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ParamListForm form = new ParamListForm();
            ShowForm(form);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox box = new AboutBox();
            box.ShowDialog(this);
        }
    }
}
